package cn.wolfcode.wolf2w.util;

import cn.wolfcode.wolf2w.query.QueryObject;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

public class DBHelp {

    public static <T> Page<T> query(MongoTemplate template, Pageable pageable, Query query, Class<T> clz, QueryObject qo) {

        long count = template.count(query, clz);

        if (count == 0 ){

            return Page.empty();
        }

        query.with(pageable);

        List<T> list = template.find(query, clz);

        return new PageImpl<>(list, pageable, count);
    }
}
